using Mapster;
using AhCha.Fortunate.ModelsDto;
using AhCha.Fortunate.Entity.MySQL;
using AhCha.Fortunate.IService.MySQL;
using AhCha.Fortunate.Repositories.SqlSugar;
using AhCha.Fortunate.ModelsDto.MySQL.ApiRequestLogDto;

namespace AhCha.Fortunate.Service.MySQL
{
    public class ApiRequestLogService(SqlSugarRepository<ApiRequestLog> TEntityRep) : BaseServices<ApiRequestLog>, IApiRequestLogService
    {
        private readonly SqlSugarRepository<ApiRequestLog> _TEntityRep = TEntityRep;

        /// <summary>
        /// 获取接口请求记录日志
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<SqlSugarPagedList<ApiRequestLogOutput>> GetApiRequestLogPage(QueryApiRequestLogInput input)
        {
            return await _TEntityRep.AsQueryable().Select<ApiRequestLogOutput>().ToPagedListAsync(input.PageIndex, input.PageSize);
        }

        /// <summary>
        /// 新增ApiRequestLog数据
        /// </summary>
        public async Task PostApiRequestLog(PostApiRequestLogInput input)
        {
            using var db = SqlSugarContext.GetInstance(GetMySQLConn);
            var entity = input.Adapt<ApiRequestLog>();
            await db.Insertable<ApiRequestLog>(entity).ExecuteCommandAsync();
        }
    }
}
